package org.example.graph_theory;

public class The_largest_area_of_the_island {
    public static void main(String[] args) {
        //岛屿的最大面积

        //给你一个大小为 m x n 的二进制矩阵 grid 。
        //岛屿 是由一些相邻的 1 (代表土地) 构成的组合，这里的「相邻」要求两个 1 必须在 水平或者竖直的四个方向上 相邻。你可以假设 grid 的四个边缘都被 0（代表水）包围着。
        //岛屿的面积是岛上值为 1 的单元格的数目。
        //计算并返回 grid 中最大的岛屿面积。如果没有岛屿，则返回面积为 0 。

//        int [][] grid = {
//                {0,0,1,0,0,0,0,1,0,0,0,0,0},
//                {0,0,0,0,0,0,0,1,1,1,0,0,0},
//                {0,1,1,0,1,0,0,0,0,0,0,0,0},
//                {0,1,0,0,1,1,0,0,1,0,1,0,0},
//                {0,1,0,0,1,1,0,0,1,1,1,0,0},
//                {0,0,0,0,0,0,0,0,0,0,1,0,0},
//                {0,0,0,0,0,0,0,1,1,1,0,0,0},
//                {0,0,0,0,0,0,0,1,1,0,0,0,0}
//        };
        int[][] grid = {{0,0,0,0,0,0,0,0}};
        int i = maxAreaOfIsland(grid);
        System.out.println(i);
    }

    public static int maxAreaOfIsland(int[][] grid) {
        int maxLand = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[i].length; j++) {
                if (grid[i][j] == 1) {
                    int land = dfs(grid, 0, i, j);
                    maxLand = Math.max(maxLand, land);
                }
            }
        }
        return maxLand;
    }

    public static int dfs(int[][] grid, int land, int i, int j) {
        if (i < 0 || i >= grid.length || j < 0 || j >= grid[i].length || grid[i][j] == 0) return 0;
        grid[i][j] = 0;
        int landtop = dfs(grid, 0, i - 1, j);
        int landbottom = dfs(grid, 0, i + 1, j);
        int landleft = dfs(grid, 0, i, j - 1);
        int landright = dfs(grid, 0, i, j + 1);

        return 1 + landtop + landbottom + landleft + landright;
    }
}
